package com.readingtree.library.security;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/14.
 */
public class SecurityConfigEnhance implements ConfigAttribute, Serializable {

    private String attribute;

    public SecurityConfigEnhance() {
    }

    public SecurityConfigEnhance(String attrib) {
        Assert.hasText(attrib, "You must provide a configuration attribute");
        this.attribute = attrib;
    }

    @Override
    public String getAttribute() {
        return this.attribute;
    }

    public boolean equals(Object obj) {
        if (obj instanceof ConfigAttribute) {
            ConfigAttribute attr = (ConfigAttribute) obj;
            return this.attribute.equals(attr.getAttribute());
        } else {
            return false;
        }
    }

    public int hashCode() {
        return this.attribute.hashCode();
    }

    public String toString() {
        return this.attribute;
    }

    public static List<ConfigAttribute> createListFromCommaDelimitedString(String access) {
        return createList(StringUtils.commaDelimitedListToStringArray(access));
    }

    public static List<ConfigAttribute> createList(String... attributeNames) {
        Assert.notNull(attributeNames, "You must supply an array of attribute names");
        ArrayList attributes = new ArrayList(attributeNames.length);
        String[] var2 = attributeNames;
        int var3 = attributeNames.length;

        for (int var4 = 0; var4 < var3; ++var4) {
            String attribute = var2[var4];
            attributes.add(new SecurityConfigEnhance(attribute.trim()));
        }

        return attributes;
    }
}
